//
//  CBattleObject.cpp
//  Monster_Martial_Arts_Version_0
//
//  Created by chiawei.tseng on 13/1/7.
//
//


#include "CObject.h"
#include "CBattleObject.h"
#include "CMonsterData.h"
#include "CCArmatureFactory.h"
#include "cocos2d.h"
#include "CAnimationSystem.h"
#include "CPlayerControl.h"
#include "CAIManager.h"
using namespace cocos2d;
using namespace MMA;

CBattleObject::CBattleObject():CObject()
{
    m_BattleData.nLv = m_data.nLv;
    m_BattleData.nHP = m_data.nHP;
    m_BattleData.nAttack = m_data.nAttack;
    m_BattleData.nDefend = m_data.nAttackSpeed;
    m_BattleData.nMoveSpeed = m_data.nMoveSpeed;
    m_BattleData.nAttackSpeed = m_data.nAttackSpeed;
    m_BattleData.nExp = m_data.nExp;
    m_BattleData.nSize = m_data.nSize;
    m_BattleData.m_teamNamber = BATTLE_TEAM_BLUE;
    
    m_pArmature = NULL;
    m_pAnimationSys = NULL;
    m_pAiManager = NULL;
    
}

bool CBattleObject::Init(CCNode* pNode , fPoint point)
{
    if (!m_pAnimationSys)
    {
        m_pAnimationSys = new CAnimationSystem();
        
        m_pAnimationSys->Init(this);
        
        //m_pAnimationSys->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_IDLE);
    }
    
    if (!m_pAiManager)
    {
        m_pAiManager = new CAIManager();
    }
    
    
    
    m_BattleData.m_fpoint.X = point.X;
    m_BattleData.m_fpoint.Y = point.Y;
    
    processSprite(pNode);
    
    return true;
}

bool CBattleObject::InitWithData(const char* Name, CCNode* pNode)
{
    MONSTER_DATA MonsterData = CMonsterDatas::Get().LoadMonsterData(Name);
    
    m_pArmature = CCArmatureFactory::Get().CreateArmature(MonsterData.ArmatureName,MonsterData.AnimationName,MonsterData.PnlName);
    
    m_pArmature->retain();
    
    CCAssert(m_pArmature, "armature can not Null when Create armature");
    
    if (!pNode)
        return false;
    
    pNode->addChild(m_pArmature->getDisplay()->getParent());
    
    return true;
    
}



void CBattleObject::Update(CCNode* pNode, float dTime)
{
    if (m_pAnimationSys)
    {
        m_pAnimationSys->Update(dTime);
    }
    
    if (m_pAiManager)
    {
        CAiTimeInput* pTime = new CAiTimeInput();
        
        pTime->dt = dTime;
        
        m_pAiManager->Update(pTime);
        
        delete pTime;
        
        pTime = NULL;
    }
    
    processSprite(pNode);
}

void CBattleObject::ChangeActiveAnimation(ACTIVE_ANIMATION_ID AnimationID)
{
    m_pAnimationSys->ChangeActiveAnimation(AnimationID);
}

void CBattleObject::Release(CCNode* pNode)
{
    CCAssert(pNode, "BattleLayer is NULL");
    
    if (m_pAnimationSys != NULL)
    {
        m_pAnimationSys->Release();
        delete m_pAnimationSys;
        m_pAnimationSys = NULL;
    }

    if (m_pArmature)
    {
        pNode->removeChild(m_pArmature->getDisplay()->getParent(), false);
        
        m_pArmature->release();
        
        m_pArmature = NULL;
    }
    
    if (m_pAiManager != NULL)
    {
        m_pAiManager->Release();
        
        delete m_pAiManager;
        
        m_pAiManager = NULL;
    }
    
    
}

void CBattleObject::processSprite(CCNode* pNode)
{
    m_pArmature->getDisplay()->getParent()->setPosition(ccp(m_BattleData.m_fpoint.X,m_BattleData.m_fpoint.Y));
    
    m_pArmature->getDisplay()->setScaleX(static_cast<int>(m_BattleData.m_fpoint.face));
    
    pNode->reorderChild(m_pArmature->getDisplay()->getParent(),ZOREDMAX-m_BattleData.m_fpoint.Y);
}

bool CBattleObject::isAABB(const fPoint& point)
{
    float Radius = m_BattleData.nSize/2;
    
    if (point.X < m_BattleData.m_fpoint.X-Radius)
    {
        return false;
    }
    else if(point.X > m_BattleData.m_fpoint.X+Radius)
    {
        return false;
    }
    else if(point.Y < m_BattleData.m_fpoint.Y-Radius)
    {
        return false;
    }
    else if(point.Y > m_BattleData.m_fpoint.Y+Radius)
    {
        return false;
    }
    
    return true;
}

void CBattleObject::processCommand(CBaseCommand* command)
{
    if (m_pAiManager)
    {
        m_pAiManager->JumpToState(command->m_State,command);
    }
}

void CBattleObject::RegisterAnimationCallBack(ACTIVE_ANIMATION_ID id,CCallBack* callback)
{
    m_pAnimationSys->AddCallBackFunction(id, callback);
}

ACTIVE_ANIMATION_ID CBattleObject::GetCurrActiveAnimation()
{
    return m_pAnimationSys->GetCurrActiveAnimationID();
}

//========================================================================================


void CBattleObject::PlayToAnimation(const char* Name)
{
    m_pArmature->getAnimation()->playTo((void*)Name);
}

void CBattleObject::UpdateAnimation(float dTime)
{
    m_pArmature->update(dTime);
}

void CBattleObject::RegisterAniEventListen(CCAnimationEventListener* pAniEventListener)
{
    m_pArmature->getAnimation()->setAniEventListener(pAniEventListener);
}

